<%-- 
    Document   : speets
    Created on : Mar 9, 2012, 4:57:31 PM
    Author     : Alberto
--%>

<%@page import="edu.harding.comp431.ImageUtilities"%>
<%@page import="edu.harding.comp431.Speet"%>
<%@page import="java.util.ArrayList"%>
<%@page import="edu.harding.comp431.Database"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<jsp:useBean id="user"
             class="edu.harding.comp431.SpitterUser"
             scope="session" />

<%
    // Get database from the application context
    Database spitterDatabase = (Database)application.getAttribute("spitterDatabase");
    if (spitterDatabase == null) {
        response.sendRedirect("ActionServlet");
        return;
    }
    
   // Make sure the user is logged in
    if (session.getAttribute("login") == null) {
        response.sendRedirect("login.jsp");
        return;
     }
%>

<span class="main-heading">Speets</span>
<br /><br />

<%
     ArrayList<Speet> speets = null;
     if (request.getParameter("limit") == null)
     {
         speets = spitterDatabase.getAllSpeets(user.getUsername());
     }
     else
     {
         speets = spitterDatabase.getSpeets(user.getUsername());
     }

    if (speets.size() == 0) {
        %>
        There are no speets.
        <%
    }
    else {
        for (Speet s : speets) {                             
            String imageFile = s.getUsername() + ".jpg"; 
            String fullPath = this.getServletContext().getRealPath(
                        "images\\" + imageFile);
            if (!ImageUtilities.imageExists(fullPath))
                imageFile = "default.jpg";                
%>

<table>
    <tr>
        <td valign="top">
            <img src="images/<%= imageFile %>" class="speet-profile-image"/>
           
        </td>
        <td width="100%">
            <span class="speet-username"><%= s.getUsername() %></span><br />
            <span class="speet-message"><%= s.getMessage() %></span><br />
            <span class="speet-timestamp"><%= s.getTimeStamp() %></span>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <hr />
        </td>
    </tr>
</table>

<% } } %>